/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.innovativeEnterprise.service;

import com.fzhucc.modules.innovativeEnterprise.domain.InnovativeEnterprise;
import com.fzhucc.modules.innovativeEnterprise.domain.dto.InnovativeEnterpriseDto;
import com.fzhucc.modules.innovativeEnterprise.domain.dto.InnovativeEnterpriseQueryCriteria;
import com.fzhucc.modules.innovativeEnterprise.repository.InnovativeEnterpriseRepository;
import com.fzhucc.modules.innovativeEnterprise.service.mapstruct.InnovativeEnterpriseMapper;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import com.fzhucc.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yyg
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2023-05-31
 **/
@Service
@RequiredArgsConstructor
public class InnovativeEnterpriseService {

    private final InnovativeEnterpriseRepository innovativeEnterpriseRepository;
    private final InnovativeEnterpriseMapper innovativeEnterpriseMapper;

    public Map<String, Object> queryAll(InnovativeEnterpriseQueryCriteria criteria, Pageable pageable) {
        Page<InnovativeEnterprise> page = innovativeEnterpriseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        Page<InnovativeEnterpriseDto> map = page.map(innovativeEnterpriseMapper::toDto);
        List<InnovativeEnterpriseDto> list = map.getContent();
        if (list != null && list.size() > 0) {
            for (InnovativeEnterpriseDto innovativeEnterpriseDto : list) {
                String introduction = innovativeEnterpriseDto.getIntroduction();
                String str = introduction.replaceAll("(\\\r\\\n|\\\r|\\\n|\\\n\\\r)", "<br>");
                if(str.contains("<br>")){
                    String s = str.replaceAll("<br>", "");
                    innovativeEnterpriseDto.setIntroduction(s);
                }else{
                    innovativeEnterpriseDto.setIntroduction(str);
                }

                String situation = innovativeEnterpriseDto.getSituation();
                String str2 = situation.replaceAll("(\\\r\\\n|\\\r|\\\n|\\\n\\\r)", "<br>");
                if(str2.contains("<br>")){
                    String s = str2.replaceAll("<br>", "");
                    innovativeEnterpriseDto.setSituation(s);
                }else{
                    innovativeEnterpriseDto.setSituation(str2);
                }
            }
        }
        return PageUtil.toPage(map);
    }

    public List<InnovativeEnterpriseDto> queryAll(InnovativeEnterpriseQueryCriteria criteria) {
        return innovativeEnterpriseMapper.toDto(innovativeEnterpriseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Transactional
    public InnovativeEnterpriseDto findById(Integer id) {
        InnovativeEnterprise innovativeEnterprise = innovativeEnterpriseRepository.findById(id).orElseGet(InnovativeEnterprise::new);
        ValidationUtil.isNull(innovativeEnterprise.getId(), "InnovativeEnterprise", "id", id);
        return innovativeEnterpriseMapper.toDto(innovativeEnterprise);
    }

    @Transactional(rollbackFor = Exception.class)
    public InnovativeEnterpriseDto create(InnovativeEnterprise resources) {
        return innovativeEnterpriseMapper.toDto(innovativeEnterpriseRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(InnovativeEnterprise resources) {
        InnovativeEnterprise innovativeEnterprise = innovativeEnterpriseRepository.findById(resources.getId()).orElseGet(InnovativeEnterprise::new);
        ValidationUtil.isNull(innovativeEnterprise.getId(), "InnovativeEnterprise", "id", resources.getId());
        innovativeEnterprise.copy(resources);
        innovativeEnterpriseRepository.save(innovativeEnterprise);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            innovativeEnterpriseRepository.deleteById(id);
        }
    }

    public void download(List<InnovativeEnterpriseDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (InnovativeEnterpriseDto innovativeEnterprise : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("企业名称", innovativeEnterprise.getName());
            map.put("来源", innovativeEnterprise.getSource());
            map.put("简介", innovativeEnterprise.getIntroduction());
            map.put("项目情况", innovativeEnterprise.getSituation());
            map.put("是否展示", innovativeEnterprise.getIsShow());
            map.put("排序", innovativeEnterprise.getSort());
            map.put("类型字典", innovativeEnterprise.getType());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}